package org.cargloss.cp.controller.modul.staticpage;

import org.cargloss.cp.model.StaticPage;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionCrud extends ActionSupport {

	private static final long serialVersionUID = -5933849517817526086L;

	private int id;

	private String title;
	private String content;
	private String initial;
	private String url;

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();
		StaticPage object = new StaticPage();
		dao.createCriteria(StaticPage.class);
		StaticPage lastId = (StaticPage) dao.lastId();
		object.setTitle(getTitle());
		object.setContent(getContent());
		object.setInitial(getInitial());
		if (lastId == null) {
			url = "page/staticpage/" + "1";
		}else
		{
			url = "page/staticpage/" + (lastId.getId()+1);
		}
		
		object.setUrl(getUrl());

		dao.insert(object);
		dao.close();
		return "empty";
	}

	public String update() {

		DAO<Integer> dao = new DAO<Integer>();
		StaticPage object = (StaticPage) dao.find(StaticPage.class,
				Integer.valueOf(getId()));

		object.setTitle(getTitle());
		object.setContent(getContent());
		object.setInitial(getInitial());
		url = "page/staticpage/" + getId();
		object.setUrl(getUrl());

		dao.update(object);
		dao.close();
		return "empty";
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();
		StaticPage delete = (StaticPage) dao.find(StaticPage.class,
				Integer.valueOf(getId()));
		dao.delete(delete);
		dao.close();
		return "empty";
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public String getInitial() {
		return initial;
	}

	public void setInitial(String initial) {
		this.initial = initial;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
